package com.voe.test.unittest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.voe.callback.InStream;

public class MyInStream extends InStream {
	
	public static MyInStream openFile(String filename) {
		
		try {
			FileInputStream stream = new FileInputStream(filename);
			return new MyInStream(stream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	protected MyInStream(FileInputStream stream) {
		filestream_ = stream;
	}
	
	@Override
	protected void finalize() {
		close();
	}
	
	public void close() {
		if(filestream_ != null) {
			try {
				filestream_.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			filestream_ = null;
		}
	}

	@Override
	protected int read(byte[] buffer) {
		int ret = -1;
		try {
			ret = filestream_.read(buffer);
			offset_ += ret;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return ret;
	}

	@Override
	protected int rewind() {
		return offset_;
	}

	protected FileInputStream filestream_;
	protected int offset_ = 0;
}
